package com.castle.fortress.admin.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsRoleChannelDto;
import com.castle.fortress.admin.cms.entity.CmsRoleChannelEntity;
import com.castle.fortress.admin.cms.mapper.CmsRoleChannelMapper;
import com.castle.fortress.admin.cms.service.CmsRoleChannelService;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * cms栏目角色关联表 服务实现类
 *
 * @author castle
 * @since 2022-06-29
 */
@Service
public class CmsRoleChannelServiceImpl extends ServiceImpl<CmsRoleChannelMapper, CmsRoleChannelEntity> implements CmsRoleChannelService {
    /**
    * 获取查询条件
    * @param cmsRoleChannelDto
    * @return
    */
    public QueryWrapper<CmsRoleChannelEntity> getWrapper(CmsRoleChannelDto cmsRoleChannelDto){
        QueryWrapper<CmsRoleChannelEntity> wrapper= new QueryWrapper();
        if(cmsRoleChannelDto != null){
            CmsRoleChannelEntity cmsRoleChannelEntity = ConvertUtil.transformObj(cmsRoleChannelDto,CmsRoleChannelEntity.class);
            wrapper.like(cmsRoleChannelEntity.getId() != null,"id",cmsRoleChannelEntity.getId());
            wrapper.like(cmsRoleChannelEntity.getRoleId() != null,"role_id",cmsRoleChannelEntity.getRoleId());
            wrapper.like(cmsRoleChannelEntity.getChannelId() != null,"channel_id",cmsRoleChannelEntity.getChannelId());
        }
        return wrapper;
    }


    @Override
    public IPage<CmsRoleChannelDto> pageCmsRoleChannel(Page<CmsRoleChannelDto> page, CmsRoleChannelDto cmsRoleChannelDto) {
        QueryWrapper<CmsRoleChannelEntity> wrapper = getWrapper(cmsRoleChannelDto);
        Page<CmsRoleChannelEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsRoleChannelEntity> cmsRoleChannelPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsRoleChannelDto> pageDto = new Page(cmsRoleChannelPage.getCurrent(), cmsRoleChannelPage.getSize(),cmsRoleChannelPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsRoleChannelPage.getRecords(),CmsRoleChannelDto.class));
        return pageDto;
    }


    @Override
    public List<CmsRoleChannelDto> listCmsRoleChannel(CmsRoleChannelDto cmsRoleChannelDto){
        QueryWrapper<CmsRoleChannelEntity> wrapper = getWrapper(cmsRoleChannelDto);
        List<CmsRoleChannelEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsRoleChannelDto.class);
    }
}

